<?php
namespace app\service;
use alipay\AlipayEbppIndustryCommunityDeliveryrecentorderDetect;
use alipay\AopClient;
use alipay\AlipayOpenAuthTokenAppRequest;    //查询物业token
use alipay\AlipaySystemOauthTokenRequest;//换取授权访问令牌
use alipay\AlipayTradeCreateRequest;//生成订单
use alipay\AlipayTradeCloseRequest;
use alipay\AlipayOpenAppMiniTemplatemessageSendRequest;//小程序模板信息
use alipay\AlipayOpenAppQrcodeCreateRequest;//创建二维码
use alipay\ZolozIdentificationCustomerCertifyzhubQueryRequest;
use alipay\ZolozIdentificationUserWebQueryRequest;
use alipay\AlipayEbppCommunityDeliveryidentityDetectRequest;
class apixcxService{

    const XCXPRIKEYPATH = 'F:\php\zaozaopay\public\xcxprikey.txt';//小程序私钥路径
    const XCXPUBKEYPATH = 'F:\php\zaozaopay\public\xcxpubkey.txt';//小程序支付宝公钥路径
    const LIFEPRIKEYPATH = '/www/wwwroot/hzymt/prikey.txt';//生活号私钥路径
    const LIFEPUBKEYPATH = '/www/wwwroot/hzymt/pubkey.txt';//生活号支付宝公钥路径

    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//

    public function getPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018031602386173';
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
     //XIOA
    public function xcxgetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2019061665593126';
        $aop->rsaPrivateKey = file_get_contents(self::XCXPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::XCXPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
    public function test($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new AlipayEbppCommunityDeliveryidentityDetectRequest ();
        $request->setBizContent("{" .
            "\"delivery_user_id\":\"$data->delivery_user_id\"," .
            "\"community_id\":\"$data->community_id\"," .
            "\"company_code\":\"$data->company_code\"," .
            "\"company_name\":\"$data->company_name\"," .
            "\"city_name\":\"$data->city_name\"," .
            "\"community_name\":\"$data->community_name\"," .
            "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    public function test2($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new AlipayEbppIndustryCommunityDeliveryrecentorderDetect ();
        $request->setBizContent("{" .
            "\"delivery_user_id\":\"$data->delivery_user_id\"," .
            "\"company_code\":\"$data->company_code\"," .
            "\"company_name\":\"$data->company_name\"," .
            "\"city_code\":\"$data->city_code\"," .
            "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    //小程序换取授权访问令牌
    public function xcxoauthToken($code){
        $aop = $this->xcxgetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ($request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";   
        }
        return $result->$responseNode;
    }

    public function lifegetPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2018032102418589';
        $aop->rsaPrivateKey = file_get_contents(self::LIFEPRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::LIFEPUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }

    //换取授权访问令牌
    public function oauthToken($code){
        $aop = $this->lifegetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        }
        return $result->$responseNode;
    }

   
    public function faceimgquery($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new ZolozIdentificationUserWebQueryRequest ();
        $request->setBizContent("{" .
        "\"biz_id\":\"$data->biz_id\"," .
        "\"zim_id\":\"$data->zim_id\"," .
        "\"extern_param\":\"{}\"" .
        "  }");
        $result = $aop->execute ( $request); 
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    public function customerquery($data){
        $data = json_decode($data);
        $aop = $this->xcxgetPublicContent();
        $request = new ZolozIdentificationCustomerCertifyzhubQueryRequest ();
        $request->setBizContent("{" .
        "\"biz_id\":\"$data->biz_id\"," .
        "\"zim_id\":\"$data->zim_id\"," .
        "\"face_type\":0," .
        "\"need_img\":true" .
        "  }");
        $result = $aop->execute ( $request); 
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
}